<?php
	
	/**
	  *	This constant defines security value that does not allow direct access to other scripts
	  *
	  *	@name SECURITY
	  */
	define( 'SECURITY', 1 );
	
	include( 'import_list.php' );
	
	$get_info = $_GET;
	
	$project = isset( $get_info['project'] ) ? $get_info['project'] : '';
	
	if ( empty( $project ) ) {
		die( 'error: no project given' );
	}
	
	if ( !isset( CmsDocumentatorConfig::$packets[$project] ) ) {
		die( 'error: no such project' );
	}
	
	$hint_file_address = CmsDocumentatorConfig::$base_address .  '/' . CmsDocumentatorConfig::$packets[$project]['address'] . '/' . CmsDocumentatorConfig::$packets[$project]['hint_file'];
	
	if ( !file_exists( $hint_file_address ) ) {
		die( 'error: hint file does not exist' );
	}
	
	$lines = SimpleFile::getLines( array( 'address' => $hint_file_address, 'external_address' => true ) );
	
	$hint_found = false;
	
	if ( !empty( $lines ) ) {
		foreach( $lines as $line ) {
			if ( strpos( $line, CmsDocumentatorConfig::$packets[$project]['hint_const'] ) !== false ) {
				if ( strpos( $line, CmsDocumentatorConfig::$packets[$project]['hint_value'] ) !== false ) {
					$hint_found = true;
					break;
				}
			}
		}
	}
	
	if ( !$hint_found ) {
		die( 'error: hint not found' );
	}
	
	$version_file_address = CmsDocumentatorConfig::$base_address .  '/' . CmsDocumentatorConfig::$packets[$project]['address'] . '/' . CmsDocumentatorConfig::$packets[$project]['version_file'];
	
	if ( !file_exists( $version_file_address ) ) {
		die( 'error: version file does not exist' );
	}
	
	$lines = SimpleFile::getLines( array( 'address' => $version_file_address, 'external_address' => true ) );
	
	$version = null;
	
	if ( !empty( $lines ) ) {
		foreach( $lines as $line ) {
			
			$line = trim( $line );
			
			if ( strpos( $line, 'define' ) === 0 ) {
				if ( strpos( $line, CmsDocumentatorConfig::$packets[$project]['version_const'] ) ) {
					
					$define_array = explode( ',', $line );
					unset( $define_array[0] );
					
					if ( empty( $define_array ) ) {
						continue;
					}
					
					$define_right = implode( ',', $define_array );
					
					$define_array = explode( ')', $define_right );
					
					if ( count( $define_array ) < 2 ) {
						continue;
					}
					
					unset( $define_array[count( $define_array ) - 1] );
					
					$define_main = trim( implode( ')', $define_array ) );
					
					if ( strlen( $define_main ) == 0 ) {
						continue;
					}
					
					if ( ( substr( $define_main, 0, 1 ) == '"' ) || ( substr( $define_main, 0, 1 ) == "'" ) ) {
						
						if ( strlen( $define_main ) < 2 ) {
							continue;
						}
						
						$version = substr( $define_main, 1, strlen( $define_main ) - 2 );
						
					} else {
						$version = $define_main;
					}
					
				}
			}
			
		}
	}
	
	if ( is_null( $version ) ) {
		die( 'error: could not find project version' );
	}
	
	$obj_documentator = new Documentator();
	
	die( $obj_documentator->performIteration( $project, $version ) );

?>